package com.jiayuge.poiStudy.service;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.ChartMultiSeriesRenderData;
import com.deepoove.poi.data.ChartSingleSeriesRenderData;
import com.deepoove.poi.data.SeriesRenderData;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图标生成
 */
public class GenerateChartFile {
    public static void main(String[] args) throws IOException {
        //生成数据模板
        XWPFTemplate template = XWPFTemplate.compile("./src/main/java/com/jiayuge/poiStudy/files/wordTemplate.docx");

        //导出路径
        FileOutputStream outputStream = new FileOutputStream("./src/main/java/com/jiayuge/poiStudy/files/chart.docx");
        HashMap<String, Object> data = new HashMap<>();

        //将数据存储为了后边生成图样式
        List<String> devname = new ArrayList<String>();
        List<Double> useRate = new ArrayList<Double>();
        List<Integer> useYear = new ArrayList<Integer>();

        //构造出需要渲染的数据
        for (int i = 0; i < 5; i++) {
            //存入list,为了生成图表
            devname.add("电灯_" + i);
            useRate.add(Math.random() * 100);
            useYear.add(i + 1);
        }

        //柱状图
        data.putAll(histogram(devname, useRate, useYear));
        //折线图
        data.putAll(lineChart(devname, useRate, useYear));
        //柱状图、线图共存
        data.putAll(coexistence(devname, useRate, useYear));
        //饼状图
        data.putAll(pieChart(devname, useRate));


        template.render(data);
        template.write(outputStream);

        outputStream.flush();
        outputStream.close();
        template.close();
    }

    private static Map<String,Object> pieChart(List<String> devname, List<Double> useRate) {
        /**
         * 创建只有只有一种数据时 用ChartSingleSeriesRenderData
         */

        ChartSingleSeriesRenderData chart = new ChartSingleSeriesRenderData();
        chart.setChartTitle("饼状图");

        //必要步骤 设置分类集合
        chart.setCategories(devname.toArray(new String[devname.size()]));

        SeriesRenderData seriesRenderData = new SeriesRenderData();
        seriesRenderData.setName("使用率abr");
        seriesRenderData.setValues(useRate.toArray(new Double[useRate.size()]));
        chart.setSeriesData(seriesRenderData);

        HashMap<String, Object> data = new HashMap<>();
        data.put("pieChart", chart);

        return data;

    }

    private static Map<String,Object> coexistence(List<String> devname, List<Double> useRate, List<Integer> useYear) {
        ChartMultiSeriesRenderData chart = new ChartMultiSeriesRenderData();
        chart.setChartTitle("柱状图、线图共存");

        //设置图例-类别
        chart.setCategories(devname.toArray(new String[devname.size()]));

        List<SeriesRenderData> series = new ArrayList<>();
        SeriesRenderData seriesRenderData1 = new SeriesRenderData();
        seriesRenderData1.setName("使用率abr");
        seriesRenderData1.setValues(useRate.toArray(new Double[useRate.size()]));
        seriesRenderData1.setComboType(SeriesRenderData.ComboType.BAR);
        series.add(seriesRenderData1);

        SeriesRenderData seriesRenderData2 = new SeriesRenderData();
        seriesRenderData2.setName("使用率line");
        seriesRenderData2.setValues(useRate.toArray(new Double[useRate.size()]));
        seriesRenderData2.setComboType(SeriesRenderData.ComboType.LINE);
        series.add(seriesRenderData2);

        SeriesRenderData seriesRenderData3 = new SeriesRenderData();
        seriesRenderData3.setName("使用年限abr");
        seriesRenderData3.setValues(useYear.toArray(new Integer[useYear.size()]));
        seriesRenderData3.setComboType(SeriesRenderData.ComboType.BAR);
        series.add(seriesRenderData3);

        SeriesRenderData seriesRenderData4 = new SeriesRenderData();
        seriesRenderData4.setName("使用年限line");
        seriesRenderData4.setValues(useYear.toArray(new Integer[useYear.size()]));
        seriesRenderData4.setComboType(SeriesRenderData.ComboType.LINE);
        series.add(seriesRenderData4);

        chart.setSeriesDatas(series);

        HashMap<String, Object> data = new HashMap<>();
        data.put("coexistence", chart);

        return data;


    }

    private static Map<String,Object> lineChart(List<String> devname, List<Double> useRate, List<Integer> useYear) {
        ChartMultiSeriesRenderData chart = new ChartMultiSeriesRenderData();
        chart.setChartTitle("折线图");

        //参数设置 基本单元 图例
        chart.setCategories(devname.toArray(new String[devname.size()]));

        //序列话需要展示的数据
        //往往有多项数据指标 所以用List来封装
        List<SeriesRenderData> seriesDatas = new ArrayList<>();

        seriesDatas.add(new SeriesRenderData("使用率",  useRate.toArray(new Double[useRate.size()])));
        seriesDatas.add(new SeriesRenderData("使用年限", useYear.toArray(new Integer[useYear.size()])));

        chart.setSeriesDatas(seriesDatas);

        HashMap<String, Object> data = new HashMap<>();
        data.put("lineChart", chart);

        return data;
    }

    //渲染柱状图
    private static Map<String, Object> histogram(List<String> devname, List<Double> useRate, List<Integer> useYear) {

        ChartMultiSeriesRenderData chart = new ChartMultiSeriesRenderData();
        chart.setChartTitle("柱状图");

        //参数设置 基本单元 图例
        chart.setCategories(devname.toArray(new String[devname.size()]));

        //序列话需要展示的数据
        //往往有多项数据指标 所以用List来封装
        List<SeriesRenderData> seriesDatas = new ArrayList<>();

        seriesDatas.add(new SeriesRenderData("使用率",  useRate.toArray(new Double[useRate.size()])));
        seriesDatas.add(new SeriesRenderData("使用年限", useYear.toArray(new Integer[useYear.size()])));

        chart.setSeriesDatas(seriesDatas);

        HashMap<String, Object> data = new HashMap<>();
        data.put("histogram", chart);

        return data;
    }
}
